package com.sth.uncenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sth.base.exceptionHandler.SthException;
import com.sth.commonutils.JwtUtils;
import com.sth.commonutils.MD5;
import com.sth.commonutils.UUIDUtil;
import com.sth.uncenter.entity.UcenterMember;
import com.sth.uncenter.entity.vo.RegisterVo;
import com.sth.uncenter.mapper.UcenterMemberMapper;
import com.sth.uncenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author sth
 * @since 2020-12-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(@RequestBody UcenterMember member) {

        // 获取登录的手机号
        String mobile = member.getMobile();
        String password = member.getPassword();
        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new SthException(2001, "登录失败");
        }
        // 判读手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember == null) {
            throw new SthException(2001, "登录失败");
        }
        // todo
        System.out.println("加密后的密码是------------ " + MD5.encrypt(password));
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new SthException(2001, "登录失败");
        }
        if (mobileMember.getIsDisabled()) {
            throw new SthException(2001, "登录失败");
        }
        // 登录成功
        // 生成token ,使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        System.out.println("登录成功了");
        return jwtToken;
    }

    /**
     * 会员注册
     *
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        if (StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new SthException(20001, "参数不能为空");
        }

        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobleCode)) {
            throw new SthException(20001, "验证码错误");
        }

        //查询数据库中是否存在相同的手机号码
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (count.intValue() > 0) {
            throw new SthException(20001, "手机号不能重复");
        }

        //添加注册信息到数据库
        UcenterMember member = new UcenterMember();
        member.setId(UUIDUtil.get4UUID());
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        this.save(member);
    }

    @Override
    public UcenterMember getLoginInfo(String memberId) {
        UcenterMember member = baseMapper.selectById(memberId);
        return member;
    }

    @Override
    public UcenterMember getByOpenid(String openid) {
        //根据openid判断
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }
    // 统计查询 某一天的注册数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
